package com.swpu.service_drink.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_drink.entity.DrinkRecord;
import com.swpu.service_drink.entity.vo.DrinkCountChartVo;
import com.swpu.service_drink.entity.vo.DrinkRecordChartDataVo;
import com.swpu.service_drink.entity.vo.DrinkRecordSearchVo;
import com.swpu.service_drink.service.DrinkRecordService;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-02-28
 */
@RestController
@RequestMapping("/service_drink/drink-record")
public class DrinkRecordController {
    @Autowired
    private DrinkRecordService drinkRecordService;
    @PostMapping("addDrinkRecord")
    @Decrypt
    @Encrypt
    public R addDrinkRecord(@RequestBody ResultEncryption resultEncryption, DrinkRecord drinkRecord,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        drinkRecordService.addDrinkRecord(openid,drinkRecord);
        return R.ok();
    }
    @PostMapping("/getDrinkRecordDataByDate")
    @Encrypt
    @Decrypt
    public R getDrinkRecordDataByDate( @RequestBody ResultEncryption resultEncryption, DrinkRecordSearchVo drinkRecordSearchVo,HttpServletRequest request ) throws ParseException {
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        List<DrinkRecord> drinkRecordData = drinkRecordService.getDrinkRecordDataByDate(openid, drinkRecordSearchVo.getQueryDate());
        return R.ok().data("drinkRecordData",drinkRecordData);
    }
    @GetMapping("/getDrinkVolumeByDate")
    public R getDrinkRecordDataByDate(HttpServletRequest request, @RequestParam String queryDate) {
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        DateTime date = DateUtil.parseDate(queryDate);
        Map map = drinkRecordService.getDrinkVolumeByDate(openid, date);
        return R.ok().data(map);
    }

    @DeleteMapping("/deleteDrinkRecordData")
    public R deleteDrinkRecordData(HttpServletRequest request,@RequestParam(name = "recordId") String recordId){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        drinkRecordService.deleteDrinkRecordData(openid,recordId);
        return R.ok();
    }
    @GetMapping("getDrinkDataToChart")
    public R getDrinkDataToChart(HttpServletRequest request ,@RequestParam(defaultValue = "7") Integer dateCount){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        List<DrinkRecordChartDataVo> drinkDataChart = drinkRecordService.getDrinkDataToChart(openid, dateCount);
        List<DrinkCountChartVo> drinkCountChart = drinkRecordService.getDrinkCountToChart(openid,  dateCount);
        return R.ok().data("drinkChartData",drinkDataChart).data("drinkCountChartData",drinkCountChart);
    }
}

